package com.frostmourne.loreoriginex;

import com.frostmourne.loreoriginex.builders.BossBarBuilder;
import com.frostmourne.loreoriginex.commands.CommandHandler;
import com.frostmourne.loreoriginex.listeners.PlayerListener;
import com.frostmourne.loreoriginex.objects.managers.LoreOriginManager;
import org.bukkit.Bukkit;
import org.bukkit.boss.BossBar;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;

public final class LoreOriginEX extends JavaPlugin {

    private static LoreOriginEX instance;
    private static LoreOriginManager originManager;
    private static FileConfiguration settingsConfig;
    private static FileConfiguration langConfig;

    public static LoreOriginEX getInstance() {
        return instance;
    }

    public static FileConfiguration getSettingsConfig() {
        return settingsConfig;
    }

    public static FileConfiguration getLangConfig() {
        return langConfig;
    }

    @Override
    public void onEnable() {
        instance = this;
        initPlugin();

        loadFiles();

    }

    public void initPlugin(){

        Bukkit.getPluginManager().registerEvents(new PlayerListener() , this);
        new CommandHandler();
        originManager = new LoreOriginManager();
    }

    public static LoreOriginManager getOriginManager() {
        return originManager;
    }

    public void loadFiles(){
        saveDefaultConfig();
        saveResource("lang/zh_cn.yml",false);
        reloadConfig();

        File settingsFile = new File(getDataFolder() , "settings.yml");
        if (!settingsFile.exists()){
            saveResource("settings.yml",false);
        }
        settingsConfig = YamlConfiguration.loadConfiguration(settingsFile);
        try {
            settingsConfig.load(settingsFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InvalidConfigurationException e) {
            throw new RuntimeException(e);
        }

        File f = new File(getDataFolder() + "/lang",getConfig().getString("Options.Lang") + ".yml");
        langConfig = YamlConfiguration.loadConfiguration(f);

        originManager.load();
        BossBarBuilder.load();

    }

    @Override
    public void onDisable() {

    }
}
